package org.jpass.dc.view.file;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.json.annotations.JSON;
import org.jpass.dc.business.project.bean.CProject;
import org.jpass.dc.business.project.logic.FileService;
import org.jpass.dc.business.project.logic.ProjectService;
import org.jpass.dc.common.util.StringUtil;
import org.jpass.dc.view.common.BasicAction;

import com.opensymphony.xwork2.Action;

public class RenameFileAction extends BasicAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7114342098101076540L;
	
	private Log log = LogFactory.getLog(RenameFileAction.class);
	
	private FileService fileService = new FileService();
	private ProjectService projectService = new ProjectService();
	
	private String nodeId;
	private Integer depth;
	private String filePath;
	private String fileName;
	
	private String msg;
	private String type;
	private boolean success = true;
	
	@JSON(serialize = false)
	public String getNodeId() {
		return nodeId;
	}

	public void setNodeId(String nodeId) {
		this.nodeId = nodeId;
	}

	@JSON(serialize = false)
	public int getDepth() {
		return depth;
	}

	public void setDepth(int depth) {
		this.depth = depth;
	}

	@JSON(serialize = false)
	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	@JSON(serialize = false)
	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	@Override
	public String execute() throws Exception {
		if (StringUtil.isBlank(fileName)) {
			msg = "New name cannot be empty.";
			type = "warng";
			success = false;
		} else if (StringUtil.isBlank(filePath)) {
			msg = "File is not exit.";
			type = "error";
			success = false;
		} else {
			try {
				String fileRealPath = null;
				if (depth == 1) {
					if (StringUtil.isBlank(nodeId)) {
						msg = "Rename fail.";
						type = "error";
						success = false;
					} else {
						CProject p = projectService.findByProjectId(nodeId);
						if (p != null) {
							fileRealPath = this.getUserRealWrokRootPath() + "/" + p.getProjectName();
							p.setProjectName(fileName);
							projectService.renameProject(p, fileRealPath, fileName);
						} else {
							msg = "File is not exit.";
							type = "error";
							success = false;
						}
					}
				} else {
					fileRealPath = this.getUserRealWrokRootPath() + "/" + filePath;
					fileService.renameFile(fileRealPath, fileName);
				}
			} catch (Exception e) {
				log.error("重命名文件时出错，错误信息为：", e);
				msg = "Rename fail.";
				type = "error";
				success = false;
			}
			
		}
		return Action.SUCCESS;
	}

}
